<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class kalender_akademik extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_kalender_akademik');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$this->load->view('kalender_akademik');
	}
	
	function grid(){
		$data = $this->mdl_kalender_akademik->getdata();
		echo $this->mdl_kalender_akademik->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_detail($id){
		echo 'Under Construction';
	}
	
	function add(){
		$data['id_tahunajaran'] = date('Y');
        $data['kd_pt'] = $this->mdl_prosedur->getKodePT();
        $data['kd_fakultas'] = '';
        $data['kd_prodi'] = '';
        $data['Nama'] = '';
        $data['TglKRSMulai'] = date('d/m/Y');
        $data['TglKRSSelesai'] = date('d/m/Y');
        $data['TglKRSOnlineMulai'] = date('d/m/Y');
        $data['TglKRSOnlineSelesai'] = date('d/m/Y');
        $data['TglUbahKRSMulai'] = date('d/m/Y');
        $data['TglUbahKRSSelesai'] = date('d/m/Y');
        $data['TglCetakKSS1'] = date('d/m/Y');
        $data['TglCetakKSS2'] = date('d/m/Y');
        $data['TglCuti'] = date('d/m/Y');
        $data['TglMundur'] = date('d/m/Y');
        $data['TglBayarMulai'] = date('d/m/Y');
        $data['TglBayarSelesai'] = date('d/m/Y');
        $data['TglKembaliUangKuliah'] = date('d/m/Y');
        $data['TglKuliahMulai'] = date('d/m/Y');
        $data['TglKuliahSelesai'] = date('d/m/Y');
        $data['TglUTSMulai'] = date('d/m/Y');
        $data['TglUTSSelesai'] = date('d/m/Y');
        $data['TglUASMulai'] = date('d/m/Y');
        $data['TglUASSelesai'] = date('d/m/Y');
        $data['TglNilai'] = date('d/m/Y');
        $data['TglAkhirKSS'] = date('d/m/Y');
        $data['SP'] = '';
        $data['Catatan'] = '';
		$data['Status'] = 'A';
		
		$data['aksi'] = 'add';
        $data['kode'] = '';
        $data['semester'] = '1';
		
		$this->load->view('kalender_akademik_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_kalender_akademik->getdataedit($kode);
		
		$data['id_tahunajaran'] = substr($r->row()->id_tahunajaran, 0, 4);
        $data['kd_pt'] = $r->row()->kd_pt;
        $data['kd_fakultas'] = $r->row()->kd_fakultas;
        $data['kd_prodi'] = $r->row()->kd_prodi;
        $data['Nama'] = $r->row()->Nama;
        
		$data['TglKRSMulai'] = FormatDateFromMysql($r->row()->TglKRSMulai);
        $data['TglKRSSelesai'] = FormatDateFromMysql($r->row()->TglKRSSelesai);
        $data['TglKRSOnlineMulai'] = FormatDateFromMysql($r->row()->TglKRSOnlineMulai);
        $data['TglKRSOnlineSelesai'] = FormatDateFromMysql($r->row()->TglKRSOnlineSelesai);
        $data['TglUbahKRSMulai'] = FormatDateFromMysql($r->row()->TglUbahKRSMulai);
        $data['TglUbahKRSSelesai'] = FormatDateFromMysql($r->row()->TglUbahKRSSelesai);
        $data['TglCetakKSS1'] = FormatDateFromMysql($r->row()->TglCetakKSS1);
        $data['TglCetakKSS2'] = FormatDateFromMysql($r->row()->TglCetakKSS2);
        $data['TglCuti'] = FormatDateFromMysql($r->row()->TglCuti);
        $data['TglMundur'] = FormatDateFromMysql($r->row()->TglMundur);
        $data['TglBayarMulai'] = FormatDateFromMysql($r->row()->TglBayarMulai);
        $data['TglBayarSelesai'] = FormatDateFromMysql($r->row()->TglBayarSelesai);
        $data['TglKembaliUangKuliah'] = FormatDateFromMysql($r->row()->TglKembaliUangKuliah);
        $data['TglKuliahMulai'] = FormatDateFromMysql($r->row()->TglKuliahMulai);
        $data['TglKuliahSelesai'] = FormatDateFromMysql($r->row()->TglKuliahSelesai);
        $data['TglUTSMulai'] = FormatDateFromMysql($r->row()->TglUTSMulai);
        $data['TglUTSSelesai'] = FormatDateFromMysql($r->row()->TglUTSSelesai);
        $data['TglUASMulai'] = FormatDateFromMysql($r->row()->TglUASMulai);
        $data['TglUASSelesai'] = FormatDateFromMysql($r->row()->TglUASSelesai);
        $data['TglNilai'] = FormatDateFromMysql($r->row()->TglNilai);
        $data['TglAkhirKSS'] = FormatDateFromMysql($r->row()->TglAkhirKSS);
        
		//$data['SP'] = $r->row()->SP;
        $data['Catatan'] = $r->row()->Catatan;
        // $data['CUID'] = $r->row()->CUID;
        // $data['CDATE'] = $r->row()->CDATE;
        // $data['MUID'] = $r->row()->MUID;
        // $data['MDATE'] = $r->row()->MDATE;
        $data['Status'] = $r->row()->Status;
		
		$data['aksi'] = 'edit';
		$data['kode'] = $r->row()->id_tahunajaran;
        $data['semester'] = substr($r->row()->id_tahunajaran, 4, 1);
		
		$this->load->view('kalender_akademik_form', $data);
	}
	
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules('id_tahunajaran', 'id_tahunajaran', 'trim|required|numeric|xss_clean');
        $this->form_validation->set_rules('kd_pt', 'kd_pt', 'trim|required|xss_clean');
        $this->form_validation->set_rules('kd_fakultas', 'kd_fakultas', 'trim|required|xss_clean');
		if($data['aksi'] == 'edit')
			$this->form_validation->set_rules('kd_prodi', 'kd_prodi', 'trim|required|xss_clean');
        $this->form_validation->set_rules('Nama', 'Nama', 'trim|required|xss_clean');
        
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = !$this->mdl_kalender_akademik->isExistKode($data);
				if ($result){
					// get all prodi
					$data['all_prodi'] = $this->mdl_kalender_akademik->get_all_prodi($data['kd_fakultas']);
					if($data['all_prodi']->num_rows()>0){
						$result = $this->mdl_kalender_akademik->InsertOnDb($data);
					}else{
						$result = false;
						$data['pesan_error'] .= 'Tidak terdapat Prodi pada kode fakultas "'.$data['kd_fakultas'].'"';
					}
				}else{
					$data['pesan_error'] .= 'Sudah terdapat Tahun Ajaran dalam database!';
				}
			}else { // edit
				if($data['kode'] == $data['id_tahunajaran'].$data['semester']){ // jika kode tidak diubah
					$result=$this->mdl_kalender_akademik->UpdateOnDb($data);
				}else{
					$result = !$this->mdl_kalender_akademik->isExistKode($data);
					if ($result){
						$result=$this->mdl_kalender_akademik->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat Tahun Ajaran dalam database!';
					}
				}
				
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	} 
	
	// fungsi delet dengan mengganti status A menjadi D
	function delete($id){
		$result = $this->mdl_kalender_akademik->DeleteOnDb($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	} 

}